package feifei.homeworkfei;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/queryUserList.action")
public class QueryUserListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 判断你是否登录过
        ServletContext context = this.getServletContext();
        // 从context取值    key=>login  value=>user值
        List<User> loginUser = (List<User>) context.getAttribute("loginUser");
        if (loginUser == null || loginUser.size() == 0) {
            // 代表loginUser没有值---->让你去登录 ---->跳转到登录页面
            resp.sendRedirect(req.getContextPath() + "/login.html");
        } else {
            // 无法判断你是否登录过   context  注册有 张三 李四  王五这个值怎么校验
            // HTTP 无状态协议   会话技术
            // session 域对象
            HttpSession session = req.getSession();
            User user1 = (User) session.getAttribute("login");
            if (user1 == null) {
                resp.sendRedirect(req.getContextPath() + "/login.html");
            } else {
                resp.sendRedirect(req.getContextPath() + "/userList.html");
            }
        }
    }

}
